package nxu.it.controller;

import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.ModelAndView;
import io.jooby.annotation.GET;
import io.jooby.annotation.Path;
import io.jooby.annotation.QueryParam;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import nxu.it.model.ApiResult;
import nxu.it.service.GreetingService;

import java.util.Map;

@Path("/")
@Singleton
public class MainController {
    GreetingService greetingService;

    @Inject
    public MainController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GET(value = "/", produces = MediaType.HTML)
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @GET(value = "/hello", produces = MediaType.HTML)
    public String hello(@QueryParam String name) {
        return greetingService.sayHello(name);
    }

    @GET(value = "/json", produces = MediaType.JSON)
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @GET(value = "/render", produces = MediaType.HTML)
    public ModelAndView render(Context ctx) {
        return new ModelAndView("test.ftl", Map.of("name", "Jack"));
    }

}
